package com.woniuxy.joylifeshop.controller;

import com.woniuxy.common.entity.ShopCompliant;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeshop.service.CommodityService;
import com.woniuxy.joylifeshop.service.CompliantService;
import com.woniuxy.joylifeshop.service.OrderService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
public class CompliantController {

    @Autowired
    CompliantService compliantService;

    @Autowired
    CommodityService commodityService;

    @Autowired
    OrderService orderService;

    /**
    * @Author SooYoo
    * @Description 创建一个退货申请
    * @Param
    * @Return
    **/
    @RequestMapping("/createRefund")
    public ResponseInformation createRefund(String orderId, HttpServletRequest request, String title, String content){
        String userId = JWTUtils.getUserId(request.getHeader("token"));

        //System.out.println(title);
        //System.out.println(content);
        System.out.println(orderId);
        ResponseInformation responseInformation = new ResponseInformation();

        try {
            //验证空值
            if (title==""){
                responseInformation.setState(201);
                responseInformation.setMsg("标题不能为空");
                return responseInformation;
            }
            if (content==""){
                responseInformation.setState(202);
                responseInformation.setMsg("内容不能为空");
                return responseInformation;
            }
            //添加一条退货
            ShopCompliant shopCompliant = new ShopCompliant();

            shopCompliant.setComId(UUID.randomUUID().toString());
            shopCompliant.setId(userId);
            shopCompliant.setContent(content);
            shopCompliant.setTitle(title);
            shopCompliant.setStatus("未处理");
            shopCompliant.setReply("正在等待商家回复");

            //修改订单中的状态为退货中
            orderService.modifyRefundStatus(orderId);

            compliantService.createOneCompliant(shopCompliant);

            responseInformation.setMsg("提交成功");
            responseInformation.setState(200);

            return  responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setMsg("fail");
            responseInformation.setState(500);

            return responseInformation;
        }


    }



}
